package com.web.onirigi.controller;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.web.onirigi.model.AdminBean;
import com.web.onirigi.service.AdminService;
import com.web.onirigi.util.BaseResponseObject;
import com.web.onirigi.util.BaseUtil;
import com.web.onirigi.util.EncrypUtil;
import com.web.onirigi.util.ParseJsonToMap;

/**
 * 管理员登陆验证
 * @author ASUS
 * 创建时间  2017年10月13日 下午9:12:20
 *
 */
@RestController
@RequestMapping("adminRest")
public class AdminRest {

	/**
	 * 日志
	 */
	private static final Logger LOGGER =LoggerFactory.getLogger(AdminRest.class);
	
	@Autowired
	private AdminService  adminService;
	
	/**
	 * 管理员操作 登陆验证
	 * @author ASUS
	 * 创建时间  2017年10月13日 下午9:15:00
	 * @param json
	 * @return
	 */
	@RequestMapping(value="login",method= RequestMethod.POST)
	public BaseResponseObject login(@RequestBody String json) {
		BaseResponseObject  object =new BaseResponseObject();
		String[] type = new String[1];
		try {
			Map map = ParseJsonToMap.parseStringToMap(json, type);
			AdminBean adminBean = JSON.parseObject(JSON.toJSONString(map), AdminBean.class);
			Map resultMap =new HashMap();
			
			if(BaseUtil.REST_RHTYPE_SELECT.equals(type[0])) {
				String  adminId = adminBean.getAdminId(); 
				
				if(StringUtils.isNotEmpty(adminId)) {
				   AdminBean   resultBean = adminService.getAdminBean(adminBean);
				   if(resultBean != null) {
					   String  salt  =resultBean.getSalt();
					   String  adminPassword = resultBean.getAdminPassword();
					   String  password = String.join(adminBean.getAdminPassword(), salt) ;
					   String  mdPassword = EncrypUtil.MD5encrpy(password);
					   
					   if(adminPassword.equals(mdPassword)) {
						   object.setResponseCode(BaseUtil.SUCCESS_CODE);
						   object.setResponseMessage(BaseUtil.ADMIN_LOGIN_SUCCESS_CONTENT);
					   }else {
						   object.setResponseCode(BaseUtil.FAILED_CODE);
						   object.setResponseMessage(BaseUtil.LOGIN_FAILED_ADMIN_PASSWORD);
					   }
				
				   }else {
					   object.setResponseCode(BaseUtil.FAILED_CODE);
					   object.setResponseMessage(BaseUtil.LOGIN_FAILED_ADMIN_ID);
				   }
				   
				}else {
					object.setResponseCode(BaseUtil.FAILED_CODE);
					object.setResponseMessage(BaseUtil.LOGIN_FAILED_ADMIN_ID);
				}
				
			}else {
				object.setResponseCode(BaseUtil.FAILED_CODE);
				object.setResponseMessage(BaseUtil.LOGIN_FAILED_RHTYPE);
			}
			
		} catch (Exception e) {
			LOGGER.error("登陆验证错误",e);
			object.setResponseCode(BaseUtil.FAILED_CODE);
			object.setResponseMessage(BaseUtil.LOGIN_FAILED_ADMIN_CONTENT);
		}
		
		return object;
	}
	
}
